package shuashuami.hb.com.http;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * Created by Administrator on 2017/7/4.
 */

public class CGetUserInfo {

    private Activity activity;
    private TextView tvMoney;
    private TextView tvUserName, tvLevel, tvId, tvLevelNum;
    private ImageView imgUserHead;
    private GlidUtil glidUtil;
    private TextView tvWaitSubmitNum, tvGoingNum, tvCompleteNum;
    private TextView tvSign;

    public CGetUserInfo(Activity activity, TextView tvMoney, TextView tvUserName,
                        TextView tvLevel, TextView tvId, TextView tvLevelNum, ImageView imgUserHead,
                        TextView tvWaitSubmitNum, TextView tvGoingNum, TextView tvCompleteNum, TextView tvSign) {
        this.activity = activity;
        this.tvMoney = tvMoney;
        this.tvUserName = tvUserName;
        this.tvLevel = tvLevel;
        this.tvId = tvId;
        this.tvLevelNum = tvLevelNum;
        this.imgUserHead = imgUserHead;
        this.tvWaitSubmitNum = tvWaitSubmitNum;
        this.tvGoingNum = tvGoingNum;
        this.tvCompleteNum = tvCompleteNum;
        this.tvSign = tvSign;
        glidUtil = new GlidUtil(activity);
    }

    public void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);
        HttpMethods.userInfoc(listener,
                shareCUserInfo.getId()
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(activity);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            tvMoney.setText(temp.getString("money"));
                            tvUserName.setText(temp.getString("username"));
                            tvLevel.setText(temp.getString("exp"));
                            tvId.setText(temp.getString("id"));
                            tvLevelNum.setText("LV" + temp.getString("level"));
                            isShow(temp.getString("waitSubmitNum"), tvWaitSubmitNum);
                            isShow(temp.getString("goingNum"), tvGoingNum);
                            isShow(temp.getString("completeNum"), tvCompleteNum);
                            if (temp.getInt("signed") == 0) {
                                tvSign.setText("立即签到");
                            } else {
                                tvSign.setText("今日已签到");
                                tvSign.setBackgroundResource(R.drawable.shape_corner_user_level_press_bg);
                            }
                            glidUtil.uploadCircle(temp.getString("avatar"), imgUserHead, R.mipmap.ssm_icon_11);
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);
                            shareCUserInfo.setUsername(temp.getString("username"));
                            shareCUserInfo.setUserAvatar(temp.getString("avatar"));
                            shareCUserInfo.setParent_id(temp.getString("parent_id"));
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(activity, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void isShow(String str, TextView textView) {
        if (str.equals("0")) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(str);
        }
    }

}
